'''
Created on 21/09/2013

@author: Hugo sama
'''
from collections import defaultdict
class InvertedIndex:
    docList = []
    invertedIndex = defaultdict(list)

    def addWordsDocument(self,tokens,document):
        if document in self.docList:
            return
        self.docList.append(document)
        docID = self.docList.index(document)
        for token in tokens:
            if token in self.invertedIndex:
                self.invertedIndex[token].append(docID)
            else:
                self.invertedIndex[token] = [docID]
        return docID
    
    def getDocumentsWithWords(self,tokens):
        if tokens[0] in self.invertedIndex:
            intersection = self.invertedIndex[tokens[0]]
        else: return []
        for token in tokens:
            if token in self.invertedIndex:
                intersection = self.getIntersection(intersection, self.invertedIndex[token])
            else: return []
        return intersection
    
    def getDocumentsNames(self,docIDs):
        documents =[]
        if not docIDs:
            return False
        for docID in docIDs:
            documents.append(self.docList[docID])
        return documents
    
    def getIntersection(self,list1,list2):
        return list(set(list1).intersection(list2))
    
    def getDisjunction(self,list1,list2):
        return list(set(list1 + list2))
    
    def getNegation(self,documents):
        allDocs = [self.docList.index(i) for i in self.docList]
        for i in documents:
            allDocs.remove(i)
        return allDocs